<script setup lang="ts">
import SList from '@/components/common/list/SList.vue'
import { useList } from '@/composables/list-composable'
import type { ResultData } from '@/api/service'
import type { Page } from '@/type/common/list-types'
import type { DashboardCardVo } from '@/type/Dashboard'
import CardInfo from '@/views/card/CardInfo.vue'
import { dashboardCardListApi } from '@/api/dashboard-api'
import { ref } from 'vue'

const viewId = 'CardList'
const { tableData, pageQuery, statement, total, tableLoading, search } = useList(
  viewId,
  getData,
  () => {}
)

const cardInfoId = ref<string | undefined>()
const cardInfoDialog = ref<boolean>(false)

function getData() {
  tableLoading.value = true
  dashboardCardListApi(pageQuery).then((response: ResultData<Page<DashboardCardVo[]>>) => {
    total.value = response.data.total
    tableData.value = response.data.modelList
    tableLoading.value = false
  }).catch(() => {
    tableLoading.value = false
  })
}

function close() {
  cardInfoDialog.value = false
  getData()
}

const methods = {
  edit: (item: DashboardCardVo) => {
    cardInfoId.value = item.id
    cardInfoDialog.value = true
  },
  add: () => {
    cardInfoId.value = undefined
    cardInfoDialog.value = true
  }
}
</script>

<template>
  <s-list
    :statement="statement"
    :data="tableData"
    :methods="methods"
    :total="total"
    :table-loading="tableLoading"
    :page-size="pageQuery.pageSize"
    @search="search"
  >
  </s-list>

  <el-dialog
    destroy-on-close
    :close-on-click-modal="false"
    v-model="cardInfoDialog"
    title="卡片信息"
    width="1200"
  >
    <card-info :close="close" :query="{ id: cardInfoId }" />
  </el-dialog>
</template>

<style scoped></style>